<?php
function GetBetContent3 ($bettype, $numbers)
{
    global $g_betname;
    global $g_bignum;
    global $g_chinayear;
    global $g_colorname;
    $name1 = array("", "单", "双", "大", "小", "红波", "蓝波", "绿波", "合单", "合双");
    switch ($bettype)
    {
        case 0 :
            return "特码 " . d2($numbers);
        case 1 :
            return "特 " . ( $numbers == 1 ? "单" : "双" );
        case 2 :
            return "特 " . ( $numbers == 1 ? "大" : "小" );
        case 3 :
            return "特 合" . ( $numbers == 1 ? "单" : "双" );
        case 4 :
            return "正码 " . d2($numbers);
        case 5 :
            return "总 " . ( $numbers == 1 ? "单" : "双" );
        case 6 :
            return "总 " . ( $numbers == 1 ? "大" : "小" );
        case 7 :
        case 8 :
        case 9 :
        case 33 :
        case 10 :
        case 32 :
        case 11 :
        case 29 :
        case 39 :
        case 40 :
        case 41 :
        case 42 :
        case 43 :
        case 44 :
        case 45 :
            $p = explode("|", $numbers);
            return $g_betname[$bettype] . "@" . $p[1] . "|" . $p[3] . "×" . $p[2] . "组";
        case 12 :
            $p = $g_betname[$bettype] . " ";
            $p1 = explode(",", $numbers);
            foreach ($p1 as $p2)
            {
                $p3 = explode("_", $p2);
                $p .= "正码" . $g_bignum[$p3[0]] . " " . $name1[$p3[1]] . "@" . $p3[2] . "|";
            }
            $p = substr($p, 0, 0 - 2);
            return $p;
        case 13 :
            return "特 " . $g_colorname[$numbers] . "波";
        case 14 :
            return "特 肖" . $g_chinayear[$numbers];
        case 15 :
            $p = explode(" ", $numbers);
            return "正码" . $g_bignum[$p[0]] . ( $p[1] == 1 ? " 单" : " 双" );
        case 16 :
            $p = explode(" ", $numbers);
            return "正码" . $g_bignum[$p[0]] . ( $p[1] == 3 ? " 大" : " 小" );
        case 17 :
            $p = explode(" ", $numbers);
            return "正码" . $g_bignum[$p[0]] . " " . $name1[$p[1]];
        case 18 :
            $p0 = "";
            $p1 = "";
            switch ($numbers)
            {
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                    $p0 = 1;
                    $p1 = $numbers;
                    break;
                case 5 :
                case 6 :
                case 7 :
                case 8 :
                    $p0 = 2;
                    $p1 = $numbers - 4;
                    break;
                case 9 :
                case 10 :
                case 11 :
                case 12 :
                    $p0 = 3;
                    $p1 = $numbers - 8;
                    break;
            }
            if ($p1 < 3)
            {
                return "半波 " . $g_colorname[$p0] . ( $p1 == 1 ? "单" : "双" );
            }
            return "半波 " . $g_colorname[$p0] . ( $p1 == 3 ? "大" : "小" );
        case 19 :
            $p1 = explode("|", $numbers);
            $p2 = explode(",", $p1[1]);
            $p3 = explode("_", $p1[0]);
            $n1 = $p3[0];
            $n2 = intval($p3[1]);
            $op = $n2 . "肖 " . ( $n1 == 1 ? "中" : "不中" ) . "|";
            $op .= $g_chinayear[$p2[0]];
            $i = 1;
            while ($i < $n2)
            {
                $op .= "," . $g_chinayear[$p2[$i]];
                ++$i;
            }
            return $op;
        case 20 :
            return "特 " . ( $numbers == 1 ? "家禽" : "野兽" );
        case 21 :
            return "一肖" . $g_chinayear[$numbers];
        case 37 :
            return "一肖" . $g_chinayear[$numbers];
        case 22 :
            return "尾数" . substr($numbers, 0 - 1);
        case 38 :
            return "尾数" . substr($numbers, 0 - 1);
        case 23 :
            $p = explode(" ", $numbers);
            return "正码 " . $g_bignum[$p[0]] . ( $p[1] == 8 ? "合单" : "合双" );
        case 24 :
            return "特码 " . d2($numbers);
        case 25 :
            $p = explode(" ", $numbers);
            return "正特" . $g_bignum[$p[0]] . " " . d2($p[1]);
        case 26 :
            $p = explode(" ", $numbers);
            return "正特" . $g_bignum[$p[0]] . " " . d2($p[1]);
        case 27 :
            $p1 = explode("|", $numbers);
            $p2 = explode(",", $p1[1]);
            $op = $g_bignum[$p1[0]] . "肖 ";
            $i = 0;
            while ($i < $p1[0])
            {
                if (0 < $i)
                {
                    $op .= ",";
                }
                $op .= $g_chinayear[$p2[$i]];
                ++$i;
            }
            return $op;
        case 28 :
            return "特尾 " . ( $numbers == 1 ? "大" : "小" );
        case 30 :
            return "合尾 " . ( $numbers == 1 ? "大" : "小" );
        case 31 :
            return "正码 " . d2($numbers);
        case 34 :
            return "总肖 " . ( $numbers == 4 ? "234" : $numbers );
        case 35 :
            return "总肖 " . ( $numbers == 1 ? "单" : "双" );
        case 36 :
            $pp = explode("|", $numbers);
            $p1 = intval(substr($pp[0], 0, 1));
            $p2 = intval(substr($pp[0], 1, 1));
            $p3 = explode(",", $pp[1]);
            $op = $g_bignum[$p1] . "尾" . ( $p2 == 1 ? "中" : "不中" );
            $i = 0;
            while ($i < $p1)
            {
                if (0 < $i)
                {
                    $op .= ",";
                }
                $op .= $p3[$i];
                ++$i;
            }
            return $op;
        case 46 :
            return "特大中小 " . $g_bigmidname[$numbers];
        case 47 :
            return "特 合" . ( $numbers == 1 ? "大" : "小" );
    }
}

function outputBrowser ()
{
    global $cc;
    global $g_conf;
    $str = ob_get_contents();
    ob_end_clean();
    echo iconv("UTF-8","gbk//IGNORE",$str);
    ob_start();
}

if (!defined("IN_OP"))
{
    exit("You can't access this file directly...");
}
global $curruser;
include_once( "class/ccharset.php" );
$cc = new CCharset();
$ui = new UI();
$tpl = new Template2();
if (isset($_SESSION['subaccount']))
{
    $ui->showErrorPage("你是子帐号，不能执行这个功能！");
    exit();
}
include( "member/news.php" );
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    $ui->showErrorPage("<p class=error>帐号已被暂停使用</p>");
    exit();
}
$userlist = GetUserList($curruser['id']);
if ($userlist == "")
{
    $ui->showErrorPage("<p>还没有建立会员！</p>");
    exit();
}
$betcount = 0;
$rs = getdb("select id,round from lotto where open='" . date("Y-m-d") . "' order by open");
if (!$rs->eof)
{
    $lotto_id = $rs->f[0];
    $round = $rs->f[1];
    $rs = getdb("select count(id) from bet where lotto_id=" . $lotto_id);
    $betcount = $rs->f[0];
}
if ($betcount == 0)
{
    echo "抱歉，本期没有任何注单资料！";
    exit();
}
$rs->close();
$dataname = G_DBDATABASE;
while (@ob_end_clean())
{
    
}
ob_start();
header("Pragma: no-cache");
header("Expires: 0");
header("Content-disposition: attachment; filename=" . substr($dataname, 0, 1) . "_" . substr($dataname, 0 - 1) . "-" . date("Ymd") . ".csv");
header("Content-type: plain/text");
$Year = date("Y");
$Month = date("m");
$Day = date("d");
$acl_name = array("", "会员", "代理", "总代", "股东", "大股");
$search = array("\x00", "\n", "\r", "\x1A");
$replace = array("\\0", "\\n", "\\r", "\\Z");
$DataCreation = strftime("%d %B %Y", mktime(0, 0, 0, $Month, $Day, $Year));
$Time = date("H:i:s");
$crlf = "\r\n";
$i = 0;
$schema_insert = "";
$table = "";
$schema_create = "";
$betArr = array("bet", "bet2");
foreach ($betArr as $key => $table)
{
    echo "" . $crlf . "";
    if ($table == "bet")
    {
        echo "会员注单明细表" . $crlf . "";
        echo "单号,会员帐号,期数,时间,下注内容,金额,赔率,盘类,IP " . $crlf . "";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.bettype,B.line_nums,B.amount,B.rate,B.disk,B.ip";
    }
    else
    {
        echo "管理走飞明细表" . $crlf . "";
        echo "单号,管理帐号,期数,时间,阶层,走飞内容,金额,赔率,IP,模式" . $crlf . "";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.acl,B.bettype,B.line_nums,B.amount,B.rate,B.ip,B.rame";
    }
    outputBrowser();
    if ($curruser['acl'] == 2)
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM " . $table . " B LEFT JOIN k_user K ON K.id = B.user_id WHERE B.lotto_id=" . $lotto_id . " and B.user_id in (" . $userlist . ") order by B.id desc") ))
        {
            exit("Can't select data from {$table}");
        }
    }
    else if ($table == "bet")
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM " . $table . " B LEFT JOIN k_user K ON K.id = B.user_id WHERE B.lotto_id=" . $lotto_id . " and B.user_id in (" . $userlist . ") order by B.id desc") ))
        {
            exit("Can't select data from {$table}");
        }
    }
    else
    {
        if (!( $result = mysql_db_query(G_DBDATABASE, "SELECT {$fields} FROM " . $table . " B LEFT JOIN k_user K ON K.id = B.user_id WHERE B.lotto_id=" . $lotto_id . " and B.uperid" . $curruser['acl'] . "=" . $curruser['id'] . " and B.user_id!=" . $curruser['id'] . " order by B.id desc") ))
        {
            exit("Can't select data from {$table}");
        }
    }
    $a = 0;
    $schema_insert = "";
    while ($row = mysql_fetch_row($result))
    {
        $schema_insert = "";
        $fqty = mysql_num_fields($result);
        $j = 0;
        while ($j < $fqty)
        {
            if (mysql_field_name($result, $j) == "lotto_id")
            {
                $schema_insert .= " " . $round . ",";
            }
            else if (mysql_field_name($result, $j) == "createtime")
            {
                $schema_insert .= " " . date("m-d H:i:s", $row[$j]) . ",";
            }
            else if (mysql_field_name($result, $j) == "acl")
            {
                $schema_insert .= " " . $acl_name[$row[$j]] . ",";
            }
            else if (mysql_field_name($result, $j) == "bettype")
            {
                $schema_insert .= " " . GetBetContent3($row[$j], $row[$j + 1]) . ",";
            }
            else if (mysql_field_name($result, $j) == "line_nums")
            {
                $schema_insert .= "";
            }
            else if (mysql_field_name($result, $j) == "disk")
            {
                $schema_insert .= " " . $g_disk_code[$row[$j]] . ",";
            }
            else if (!isset($row[$j]))
            {
                $schema_insert .= " NULL,";
            }
            else if ($row[$j] != "")
            {
                $schema_insert .= " " . str_replace($search, $replace, addslashes($row[$j])) . ",";
            }
            else
            {
                $schema_insert .= " ,";
            }
            ++$j;
        }
        $schema_insert = ereg_replace(",\$", "", $schema_insert);
        $schema_insert .= "" . $crlf;
        echo $schema_insert;
        ++$a;
        outputBrowser();
    }
    ++$i;
}
?>